<?php

if (!defined('EMLOG_ROOT')) {
    exit('error!');
}

if (isset($_GET['do']) && $_GET['do'] == 'ractiv') {
    emer_share_ractive();
}

if (isset($_GET['do']) && $_GET['do'] == 'activ') {
    emer_share_change();
}

if (isset($_GET['do']) && $_GET['do'] == 'sync') {
    emer_share_sync();
}

function plugin_setting_view() {
    include_once 'emer_share_view_base.php';
    if (defined('EMEER_SHARE_ID') && !isset($_GET['code'])) {
        include_once 'emer_share_view_sync.php';
    } else if (isset($_GET['code']) && $_GET['code'] == 2) {
        include_once 'emer_share_view_ractiv.php';
    } else if(isset($_GET['code']) && $_GET['code'] == 5){
        include_once 'emer_share_view_sync.php';
    }else{
        include_once 'emer_share_view_activ.php';
    }
}

function plugin_setting() {
global $CACHE;
    if (isset($_POST['sync'])) {
        $sync = htmlspecialchars($_POST['sync'], ENT_QUOTES);
		$category = htmlspecialchars($_POST['category'], ENT_QUOTES);
		$ralecount = htmlspecialchars($_POST['ralecount'], ENT_QUOTES);
		emer_share_post("emer/update",array("categoryid"=>$category));
		$rale = false;
		if(isset($_POST['rale'])){
			$rale = true;
		}
		
		$emer_share_config_current = Option::get("emer_share_config");
		$emer_share_config_current = unserialize($emer_share_config_current);
			
		$emer_share_config = array(
			"EMEER_SHARE_SYNC"=>$sync,
			"EMEER_SHARE_RALATENUM"=>$ralecount,
			"EMEER_SHARE_RALATE"=>$rale,
			"EMEER_SHARE_CATEGORY"=>$category
		);
		
        $emer_share_new_config = array_merge($emer_share_config_current,$emer_share_config );
		Option::updateOption('emer_share_config',serialize($emer_share_new_config));
		$CACHE->updateCache("options");
    } else {
        emer_share_active();
    }
}

function emer_share_active() {
    global $CACHE;
    $emer_user_cache = $CACHE->readCache('user');

    $emer_share_post_url = EMER_API_URL."emer/active";
    $emer_share_ch = curl_init();
    if (!empty($emer_user_cache[1]['mail'])) {
        $emer_post_data = array(
            'email' => $emer_user_cache[1]['mail'],
            'blogname' => Option::get('blogname'),
            'blogdes' => Option::get('bloginfo'),
            'blogurl' => BLOG_URL,
            'emername' => $emer_user_cache[1]['name'],
            'emer_avatar' => md5($emer_user_cache[1]['mail'])
        );

        curl_setopt($emer_share_ch, CURLOPT_URL, $emer_share_post_url);
        curl_setopt($emer_share_ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($emer_share_ch, CURLOPT_POST, 1);
        curl_setopt($emer_share_ch, CURLOPT_POSTFIELDS, $emer_post_data);

        $emer_share_output = curl_exec($emer_share_ch);
		if ($emer_share_output === FALSE) {
			emMsg("cURL Error: " . curl_error($emer_share_ch));
		}
		
		$emer_reslut = json_decode($emer_share_output, true);
		if ($emer_reslut["status"]) {
			$emer_share_config = Option::get("emer_share_config");
			$emer_share_config = unserialize($emer_share_config);
			$profile = array(
				"EMEER_SHARE_ID"=>$emer_reslut["data"]['id'],
				"EMEER_SHARE_PW"=>$emer_reslut["data"]['pw']
			);
			$emer_share_config['PROFILE'] = $profile;
			Option::updateOption('emer_share_config',serialize($emer_share_config));
			$CACHE->updateCache("options");
		} else {
			if ($emer_reslut["info"] == 'h') {
				header("Location: ./plugin.php?plugin=emer_share&error=true&code=2");
				exit;
			}
			header("Location: ./plugin.php?plugin=emer_share&code=0");
			exit;
		}	
    }else{
		header("Location: ./plugin.php?plugin=emer_share&error=true&code=1");
		exit;
    }
}

function emer_share_ractive() {
    $emer_share_post_url = EMER_API_URL."emer/ractive";
    $emer_post_data = array(
        'blogurl' => BLOG_URL
    );
    $emer_share_ch = curl_init();
    curl_setopt($emer_share_ch, CURLOPT_URL, $emer_share_post_url);
    curl_setopt($emer_share_ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($emer_share_ch, CURLOPT_POST, 1);
    curl_setopt($emer_share_ch, CURLOPT_POSTFIELDS, $emer_post_data);

    $emer_share_output = curl_exec($emer_share_ch);
	if ($emer_share_output === FALSE) {
			emMsg("cURL Error: " . curl_error($emer_share_ch));
    }
    $emer_reslut = json_decode($emer_share_output, true);
    if ($emer_reslut['status']) {
		header("Location: ./plugin.php?plugin=emer_share&code=2&info=1");
		exit;
    }else{
		header("Location: ./plugin.php?plugin=emer_share&code=4");
		exit;
	}
}

function emer_share_change() {
global $CACHE;
    $emer_mid = $_POST['mid'];
    $emer_pw = $_POST['pw'];
    $emer_share_config = Option::get("emer_share_config");
	$emer_share_config = unserialize($emer_share_config);
	$profile = array(
		"EMEER_SHARE_ID"=>$emer_mid,
		"EMEER_SHARE_PW"=>$emer_pw
	);
	$emer_share_config['PROFILE'] = $profile;
	Option::updateOption('emer_share_config',serialize($emer_share_config));
	$CACHE->updateCache("options");
	header("Location: ./plugin.php?plugin=emer_share&code=5");
	exit;
}

function emer_share_sync(){
	global $CACHE;
    $emer_user_cache = $CACHE->readCache('user');
	if (!empty($emer_user_cache[1]['mail'])) {
		$emer_post_data = array(
			'email' => $emer_user_cache[1]['mail'],
			'blogname' => Option::get('blogname'),
			'blogdes' => Option::get('bloginfo'),
			'emername' => $emer_user_cache[1]['name'],
			'emer_avatar' => md5($emer_user_cache[1]['mail'])
		);
		$reslut = emer_share_post("emer/update",$emer_post_data);
		$reslut = json_decode($reslut,true);
		if ($reslut['status']) {
			header("Location: ./plugin.php?plugin=emer_share&info=6");
			exit;
		}else{
			header("Location: ./plugin.php?plugin=emer_share&info=7");
			exit;
		}
	}else{
		header("Location: ./plugin.php?plugin=emer_share&error=true&code=1");
		exit;
    }
}
?>